<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
//扫码上传小程序，先进入上传页面读秒，由上传页面发起获取二维码请求，获取成功后结束读秒，显示二维码。并提醒客户授权成功后点击按钮查询上传结果。
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
$return=array();
//判断上传权限
$no_upwxapp=pdo_getcolumn('core_account_group',array('id'=>$_SESSION['account']['groupid']),'no_upwxapp');
if($no_upwxapp){
	exi('您当前的会员套餐，不允许发布小程序！','','goback');	
}
if($_RQ['postdata']){
		$post=get_postdata();
		$res=cloud_request('wxapp.release2019.upload',$post);
		if($res=='time_out'){
			exi('上传失败，登陆超时！请稍后再次上传，记得及时扫码提交哦！','error','goback');
		}
		unset($_SESSION['wxapp_up']);
		exi('打包成功，请下载代码包，手动上传！','','wxapp.up_list/');
}
if($_RQ['op']=='get'){
	//$return=array();
	exi($return);
}
if($_RQ['op']=='ewm'){
	$id=$_SESSION['wxapp_up']['id'];
	$item=pdo_get('core_account_wxapp',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if(!$item['appid'] || !$item['title']){
		exi('请先设置好微信小程序APPID，标题等相关参数！','error','goback');
	}
	if(substr($item['url'],11)=='./index.php'){
		$pre=get_account_url();
		$item['url']=$pre.'custom'.substr($item['url'],1);
	}
	$item['setting']=$item['setting']?json_decode($item['setting'],true):array();
	$tpl=pdo_get('core_account_web_tpl',array('web_id'=>$item['web_id'],'is_open'=>1,'tpl_id'=>0,'uniacid'=>$_SESSION['uniacid']),array('id','web_id','items','setting','title'));
	$tpl['setting']=$tpl['setting']?json_decode($tpl['setting'],true):array();
	$tpl['setting']['window']=$item['setting']['window'];
	$tpl['setting']['live']=$item['setting']['live'];
	$params=array(
		'appid'=>$item['appid'],
		'type'=>$item['type'],
		'is_wz'=>$item['is_wz'],
		'title'=>$item['title'],
		'logo'=>tomedia($item['logo']),
		'siteroot'=>get_account_url(),
		'uniacid'=>$_SESSION['uniacid'],
		'wxapp_id'=>$item['id'],
		'att_url'=>ATTACHMENT_URL,
		'items'=>$tpl['items']?:$item['items'],
		'setting'=>ijson_encode($tpl['setting']),
		'url'=>$item['url'],
		'web_id'=>$item['web_id'],
		'isdiyindex'=>$item['isdiyindex'],
		'plugin_name'=>$item['plugin_name'],
		'istabbar'=>$item['istabbar'],
		'sharetitle'=>$item['sharetitle'],
		'sharedesc'=>$item['sharedesc'],
		'shareimg'=>tomedia($item['shareimg']),
	);
	if($item['type']==8){
		//多页小程序，读取额外小程序页面
		$params['pages']=pdo_getall('core_account_web_tpl',array('tpl_id'=>$tpl['id'],'uniacid'=>$_SESSION['uniacid']));
		$params['pages'][]=pdo_get('core_account_web_tpl',array('id'=>$tpl['id'],'uniacid'=>$_SESSION['uniacid']));
		if($params['plugin_name'] !='core'){
			$params['cloud_id']=pdo_getcolumn('core_wxapp',array('name'=>$item['plugin_name']),'cloud_id');
			if(empty($params['cloud_id'])){
				exi('未安装本应用小程序，请联系管理员！','error');
			}
		}
	}
	if($item['type']==2){
		//应用小程序，读取应用ID
		if($params['plugin_name'] !='core'){
			$params['cloud_id']=pdo_getcolumn('core_wxapp',array('name'=>$item['plugin_name']),'cloud_id');
			if(empty($params['cloud_id'])){
				exi('未安装本应用小程序，请联系管理员！','error');
			}
		}
		
	}
	$menu=pdo_getcolumn('core_diy_menu',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'plugin'=>'core'),'data');
	if($menu){
		$menu=json_decode($menu,true);
		$params['menu']=$menu;
	}
	if($params['is_wz']){
		$open_user=pdo_get('core_open_user',array('id'=>$_SESSION['openid']));
		if($open_user){
			$params['uniacid']=$open_user['open_uid'];
			$params['siteroot']=pdo_getcolumn('core_open',array('id'=>$open_user['open_id']),'siteroot');
			if(empty($params['siteroot'])){
				exi('请先设置第三方平台域名,联系管理员设置！','error','goback');
			}
		}else{
			exi('非第三方进入，不可上传支持第三方的小程序！','error','goback');
		}
				
	}
	pdo_update('core_account_wxapp',array('uptime'=>TIMESTAMP,'version'=>$_SESSION['wxapp_up']['version']),array('id'=>$_SESSION['wxapp_up']['id'],'uniacid'=>$_SESSION['uniacid']));
	$post=array('params'=>base64_encode(ijson_encode($params)));
	$res=cloud_request('wxapp.release2019.login',$post);
	if(is_error($res)){
		exi($res['message'],'error','goback');
	}
	$res['version']=$_SESSION['wxapp_up']['version'];
	$res['desc']=$_SESSION['wxapp_up']['desc'];
	$return['item']=$res;	
	exi($return);
}

